package com.kylin.system.controller;

import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.util.DateTimeUtils;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.common.poi.excel.EasyExcelUtil;
import com.kylin.system.common.utils.Constants;
import com.kylin.system.entity.SysLogininfor;
import com.kylin.system.service.SysLogininforService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 系统访问记录
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/logininfor")
public class SysLogininforController
{
    /** 登陆时间数据库字段 */
    private static final String ACCESS_TIME_FILED = "access_time";

    @Autowired
    private SysLogininforService logininforService;

    /**
     * 登录日志列表查询
     */
//    @PreAuthorize("@ss.hasPermi('system:logininfor:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysLogininfor>> list(SysLogininfor logininfor)
    {
        // 构造查询
        logininfor.getQueryWrapper()
                .like(StringUtils.isNotBlank(logininfor.getUserName()), "user_name", logininfor.getUserName())
                .eq(StringUtils.isNotBlank(logininfor.getStatus()), "status", logininfor.getStatus());

        Page<SysLogininfor> page = new Page<>();
        page = logininforService.listPage(page, logininfor);
        return ResultBean.onSuccess(page);
    }

    /**
     * 新增登录日志
     */
    @PostMapping
    public ResultBean<Object> add(@RequestParam("username") String username, @RequestParam("status") String status,
                          @RequestParam("message") String message)
    {
//        String ip = IpUtils.getRemoteAddr(ServletUtils.getRequest());

        // 封装对象
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName(username);
//        logininfor.setIpaddr(ip);
        logininfor.setMsg(message);
        // 日志状态
        if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status))
        {
            logininfor.setStatus("0");
        }
        else if (Constants.LOGIN_FAIL.equals(status))
        {
            logininfor.setStatus("1");
        }

        logininfor.perInsert();
        logininforService.insert(logininfor);

        return ResultBean.onSuccess();
    }

    /**
     * 删除登录日志
     */
//    @PreAuthorize("@ss.hasPermi('system:logininfor:remove')")
    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public ResultBean<Object> remove(@PathVariable Long[] infoIds)
    {
        Assert.isBlank("请选择需要删除的数据", infoIds);
        int count = logininforService.deleteBatchIds(Arrays.asList(infoIds));
        return ResultBean.onSuccess().put("count", count);
    }

    /**
     * 清空登录日志
     */
//    @PreAuthorize("@ss.hasPermi('system:logininfor:remove')")
    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public ResultBean<Object> clean()
    {
        logininforService.cleanLogininfor();
        return ResultBean.onSuccess();
    }

    /**
     * 导出登录日志
     */
    @Log(title = "登陆日志", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) throws IOException
    {
        // 构造查询
        logininfor.getQueryWrapper()
                .like(StringUtils.isNotBlank(logininfor.getUserName()), "user_name", logininfor.getUserName())
                .eq(StringUtils.isNotBlank(logininfor.getStatus()), "status", logininfor.getStatus());
        List<SysLogininfor> list = logininforService.list(logininfor);
        EasyExcelUtil.exportExcel(list, "登陆日志汇总", "登陆日志", SysLogininfor.class, "登陆日志导出", response);
    }

}
